package com.xbongbong.pro.message.pushset;/*/*/


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.message.domain.entity.PushSetEntity;
import com.xbongbong.pro.message.base.AbstractPushSet;
import com.xbongbong.pro.message.constant.PushSetConstant;
import com.xbongbong.pro.message.enums.PushObjectTypeEnum;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.model.PushSetModel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 审批消息设置
 *
 * @author GuoJun.HU
 * @since V1.0
 * @date 2019/2/19 10:37
 */
public class ApprovalPushSet extends AbstractPushSet {

    private String pushObject;



    public String getPushObject() {
        return pushObject;
    }



    public void setPushObject(String pushObject) {
        this.pushObject = pushObject;
    }

    @Override
    public PushSetEntity toPushSetEntity(String pushObject, String pushTime,
                                         String attrEntity) {
        setPushObject(pushObject);

        PushSetEntity pushSetEntity = new PushSetEntity();

        pushSetEntity.setAddTime(addTime);
        pushSetEntity.setAttrEntity(attrEntity);
        pushSetEntity.setCorpid(corpid);
        pushSetEntity.setDel(del);
        pushSetEntity.setEnable(enable);
        pushSetEntity.setId(id);
        pushSetEntity.setPushObject(pushObject);
        pushSetEntity.setPushTime(pushTime);
        pushSetEntity.setRefEntity(refEntity);
        pushSetEntity.setSubType(subType);
        pushSetEntity.setType(type);
        pushSetEntity.setUpdateTime(updateTime);
        return pushSetEntity;
    }

    @Override
    public PushSetEntity getPushSet(String corpid, Integer subType,
                                    PushSetModel pushSetModel) {
        Integer type = PushTypeEnum.APPROVAL_PUSH.getCode();

        Map<String,Object> param = new HashMap<>(16);
        param.put("corpid", corpid);
        param.put("type", type);
        param.put("subType", subType);

        List<PushSetEntity> pushSetList = pushSetModel.findEntitys(param);

        PushSetEntity pushSetEntity;
        if(pushSetList.isEmpty()){
            pushSetEntity = new PushSetEntity();
            pushSetEntity.setType(type);
            pushSetEntity.setSubType(subType);
            pushSetEntity.setEnable(1);
            JSONObject pushObject = new JSONObject();
            if (Objects.equals(PushTypeEnum.APPROVAL_ADD_PUSH.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.APPROVAL_ADD_PUSH.getName());
                pushSetEntity.setTitle(I18nMessageUtil.getMessage(PushSetConstant.APPROVAL_ADD));
                //user
                JSONObject userObject = new JSONObject();
                userObject.put("enable", 0);
                userObject.put("value", new ArrayList<>());
                pushObject.put("user", userObject);
            } else if (Objects.equals(PushTypeEnum.APPROVAL_NODE_CHANGE_PUSH.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.APPROVAL_NODE_CHANGE_PUSH.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.APPROVAL_NODE_CHANGE));
                //type
                JSONArray typeArray = new JSONArray();
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.put("name", PushObjectTypeEnum.CREATOR.getName());
                jsonObject1.put("enable", 1);
                jsonObject1.put("alias", PushObjectTypeEnum.CREATOR.getAlias());
                typeArray.add(jsonObject1);
                pushObject.put("type", typeArray);
                //user
                JSONObject userObject = new JSONObject();
                userObject.put("enable", 0);
                userObject.put("value", new ArrayList<>());
                pushObject.put("user", userObject);
            } else if (Objects.equals(PushTypeEnum.APPROVAL_AGREE_PUSH.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.APPROVAL_AGREE_PUSH.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.APPROVAL_AGREE));
                //type
                JSONArray typeArray = new JSONArray();
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.put("name", PushObjectTypeEnum.CREATOR.getName());
                jsonObject1.put("enable", 1);
                jsonObject1.put("alias", PushObjectTypeEnum.CREATOR.getAlias());
                typeArray.add(jsonObject1);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("name", PushObjectTypeEnum.COPYER.getName());
                jsonObject.put("alias", PushObjectTypeEnum.COPYER.getAlias());
                jsonObject.put("enable", 1);
                jsonObject.put("editUnenble", 1);
                typeArray.add(jsonObject);
                pushObject.put("type", typeArray);

            } else if (Objects.equals(PushTypeEnum.APPROVAL_REFUSE_PUSH.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.APPROVAL_REFUSE_PUSH.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.APPROVAL_REFUSE));
                //type
                JSONArray typeArray = new JSONArray();
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.put("name", PushObjectTypeEnum.CREATOR.getName());
                jsonObject1.put("enable", 1);
                jsonObject1.put("alias", PushObjectTypeEnum.CREATOR.getAlias());
                typeArray.add(jsonObject1);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("name", PushObjectTypeEnum.COPYER.getName());
                jsonObject.put("alias", PushObjectTypeEnum.COPYER.getAlias());
                jsonObject.put("enable", 1);
                jsonObject.put("editUnenble", 1);
                typeArray.add(jsonObject);
                pushObject.put("type", typeArray);
            } else if (Objects.equals(PushTypeEnum.APPROVAL_REEDIT_PUSH.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.APPROVAL_REEDIT_PUSH.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.APPROVAL_REEDIT));
                //user
                JSONObject userObject = new JSONObject();
                userObject.put("enable", 0);
                userObject.put("value", new ArrayList<>());
                pushObject.put("user", userObject);
            }
            pushSetEntity.setPushObject(pushObject.toJSONString());
        } else {
            pushSetEntity = pushSetList.get(0);
            if (Objects.equals(PushTypeEnum.APPROVAL_ADD_PUSH.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.APPROVAL_ADD_PUSH.getName());
                pushSetEntity.setTitle(I18nMessageUtil.getMessage(PushSetConstant.APPROVAL_ADD));

            } else if (Objects.equals(PushTypeEnum.APPROVAL_NODE_CHANGE_PUSH.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.APPROVAL_NODE_CHANGE_PUSH.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.APPROVAL_NODE_CHANGE));

            } else if (Objects.equals(PushTypeEnum.APPROVAL_AGREE_PUSH.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.APPROVAL_AGREE_PUSH.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.APPROVAL_AGREE));

            } else if (Objects.equals(PushTypeEnum.APPROVAL_REFUSE_PUSH.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.APPROVAL_REFUSE_PUSH.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.APPROVAL_REFUSE));

            } else if (Objects.equals(PushTypeEnum.APPROVAL_REEDIT_PUSH.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.APPROVAL_REEDIT_PUSH.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.APPROVAL_REEDIT));

            }
        }
        return pushSetEntity;
    }



    @Override
    public JSONObject getTimeJson(JSONObject jsonObject) {
        return new JSONObject();
    }



    @Override
    public PushSetEntity getPushSet(String corpid, String attr,
                                    PushSetModel pushSetModel) {
        return null;
    }

    @Override
    public List<PushSetEntity> getPushSet(String corpid, List<String> attrList, PushSetModel pushSetModel, Long templateId) {
        return null;
    }

}
